package service

import (
	"gin-bms/global"
	"gin-bms/model"
	"gin-bms/model/request"
)

//创建字典详情数据
func CreateSysDictionaryDetail(sysDictionaryDetail model.SysDictionaryDetail) (err error) {
	err = global.GLOBAL_DB.Create(&sysDictionaryDetail).Error
	return err
}

//删除字典详情数据
func DeleteSysDictionaryDetail(sysDictionaryDetail model.SysDictionaryDetail) (err error) {
	err = global.GLOBAL_DB.Delete(sysDictionaryDetail).Error
	return err
}

//更新字典详情数据
func UpdateSysDictionaryDetail(sysDictionaryDetail model.SysDictionaryDetail) (err error) {
	err = global.GLOBAL_DB.Save(sysDictionaryDetail).Error
	return err
}

//根据id获取字典详情单条数据
func GetSysDictionaryDetail(id uint) (err error, sysDictionaryDetail model.SysDictionaryDetail) {
	err = global.GLOBAL_DB.Where("id=?", id).First(&sysDictionaryDetail).Error
	return err, sysDictionaryDetail
}

//分页获取字典详情列表
func GetSysDictionaryDetailList(info request.SysDictionaryDetailSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	db := global.GLOBAL_DB.Model(&model.SysDictionaryDetail{})
	var sysDictionaryDetals []model.SysDictionaryDetail
	if info.Label != "" {
		db = db.Where("label LIKE ?", "%"+info.Label+"%")
	}
	if info.Value != 0 {
		db = db.Where("value =?", info.Value)
	}
	if info.Status != nil {
		db = db.Where("status=?", info.Status)
	}
	if info.SysDictionaryID != 0 {
		db = db.Where("sys_dictionary_id=?", info.SysDictionaryID)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&sysDictionaryDetals).Error
	return err, sysDictionaryDetals, total
}
